package com.zyq.ssyx.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyq.ssyx.acl.mapper.AdminRoleMapper;
import com.zyq.ssyx.acl.mapper.RoleMapper;
import com.zyq.ssyx.acl.service.RoleService;
import com.zyq.ssyx.model.acl.AdminRole;
import com.zyq.ssyx.model.acl.Role;
import com.zyq.ssyx.vo.acl.RoleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author clap of thunder
 * @date 2023-11-23 18:10
 * @description
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    //ServiceImpl中自动注入了baseMapper
    @Autowired
    RoleMapper roleMapper;

    @Autowired
    AdminRoleMapper adminRoleMapper;

//    @Override
//    public IPage<Role> selectPage(Page<Role> pageParam, RoleQueryVo roleQueryVo) {
//        //创建条件构造器对象
//        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
//        //模糊查询的入参从vo中获取
//        String roleName = roleQueryVo.getRoleName();
//        //roleName为空就是全查 此时分页无用
//        if (!StringUtils.isEmpty(roleName)){
//            wrapper.like(Role::getRoleName,roleName);
//        }
//        //调用mapper方法查询
//        IPage<Role> iPage = baseMapper.selectPage(pageParam, wrapper);
//        return iPage;
//    }

    //角色分页列表
    @Override
    public IPage<Role> selectPage(Page<Role> pageParam, RoleQueryVo roleQueryVo) {
        //获取条件值：角色名称
        String roleName = roleQueryVo.getRoleName();
        //创建条件构造器对象
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        //判断条件值是否为空
        if(!StringUtils.isEmpty(roleName)) {
            //封装条件
            wrapper.like(Role::getRoleName,roleName);
        }
        //调用mapper方法实现条件分页查询
        IPage<Role> pageModel = baseMapper.selectPage(pageParam, wrapper);
        return pageModel;
    }

    @Override
    public Map<String, Object> getRolesByAdminId(Long adminId) {
        HashMap<String, Object> resultMap = new HashMap<>();

        //1.根据roleId全查role表得到list<Role>
        List<Role> roleList = roleMapper.selectList(new QueryWrapper<>(null));
        List<AdminRole> existUserRoleList = adminRoleMapper.selectList(new QueryWrapper<AdminRole>().eq("admin_id", adminId).select("role_id"));
        //根据待查adminID得到的RoleList中的roleId得到Role
        Stream<AdminRole> stream = existUserRoleList.stream();
//        Stream<Object> objectStream = stream.map().collect();

        List<Long> existRoleList = existUserRoleList.stream().map(c->c.getRoleId()).collect(Collectors.toList());

        //2.根据adminId查adminRole表--->List<adminRole>--->List<Role.id>(该用户已被分配的角色对应的角色ID)
//        QueryWrapper<AdminRole> wrapper = new QueryWrapper<>();
//        wrapper.eq("admin_id",adminId).select("role_id");
//        List<AdminRole> adminIdList = adminRoleMapper.selectList(wrapper);
        //3.根据List<adminRole>中的每个roleId查list<Role>---再存进一个新List<Role>
//        ArrayList<Role> newList = new ArrayList<>();
//        for (Role role : roleList) {
//            for (AdminRole adminRole : adminIdList) {
//                if (adminRole.getRoleId().equals(role.getId())){
//                    newList.add(role);
//                }
//            }
//        }
        //4.封进map
        resultMap.put("allRoles", roleList);
        resultMap.put("assignRoles", newList);
        return resultMap;
    }
//    public Map<String, Object> getRolesByAdminId(Long adminId) {
//        //1.根据roleId全查role表得到list<Role>
//        List<Role> allRolesList = baseMapper.selectList(null);
//        //2.根据adminId查adminRole表--->List<adminRole>--->List<Role.id>(该用户已被分配的角色对应的角色ID)
//        //拥有的角色id 根据admin_id查出对应的role_id的list
//        QueryWrapper<AdminRole> wrapper = new QueryWrapper<AdminRole>();
//        QueryWrapper<AdminRole> admin_idWrapper = wrapper.eq("admin_id", adminId);
//        admin_idWrapper.select("role_id");
//        List<AdminRole> existRoleList = adminRoleService.list(admin_idWrapper);
//        //用流的方式
//        List<Long> existRoleIdList = existRoleList.stream().map(item->item.getRoleId()).collect(Collectors.toList());
//        //3.根据List<adminRole>中的每个roleId查list<Role>---再存进一个新List<Role>
//        List<Role> assignRoles = new ArrayList<>();
//        for (Role role : allRolesList) {
//            if (existRoleIdList.contains(role.getId())){
//                assignRoles.add(role);
//            }
//        }
//        //4.封进map
//        HashMap<String, Object> resultMap = new HashMap<>();
//        resultMap.put("allRoles",allRolesList);
//        resultMap.put("assignRoles",assignRoles);
//        return resultMap;
//    }
}
